import Router from "@koa/router";
import {
  loginController,
  LoginModel,
  registerController,
  userInfoController,
  refreshController
} from "../controller/auth";
import { UserAttributesWithRoles } from "../db/models/User.model";
const router = new Router({
  prefix: "/api/auth",
});

/**
 * 用户注册接口
 * /auth/register
 */
router.post("/register", async (ctx) => {
  ctx.body = await registerController(
    ctx.request.body as UserAttributesWithRoles
  );
});

/**
 * 用户登录
 * /auth/login
 */

router.post("/login", async (ctx) => {
  ctx.body = await loginController(ctx.request.body as LoginModel);
});

router.post("/info", async (ctx) => {
  const token = ctx.header.authorization || (ctx.request.body as any).token;
  ctx.body = await userInfoController(token);
});
// 新增刷新令牌的接口
router.post("/refresh-token", async (ctx) => {
  const refreshToken = ctx.header.authorization || (ctx.request.body as any).refreshToken;
  ctx.body = await refreshController(refreshToken);
});
export default router;
